<?php

namespace nimo\common\validator;

use Illuminate\Support\MessageBag;
use Illuminate\Validation\Validator;

class BatchValidator extends Validator {
    protected $index = - 1;
    public function __construct($rules, array $messages = array(), array $customAttributes = array()) {
        parent::__construct(app('translator'), array(), $rules, $messages, $customAttributes);
        $this->messages = new MessageBag();
    }
    protected function getValue($attribute) {
        return $this->data[$attribute];
    }
    protected function getAttribute($attribute) {
        return $this->data[$attribute] . " (" . ($this->index + 1) . "," . ($attribute + 1) . ") ";
    }
    public function passesRow($index, array $data) {
        $this->data = $data;
        $this->index = $index;
        $count = count($this->messages->all());
        foreach ( $this->rules as $attribute => $rules ) {
            if ($rules != null) {
                foreach ( $rules as $rule ) {
                    $this->validate($attribute, $rule);
                }
            }
        }
        return (count($this->messages->all()) - $count) === 0;
    }
    public function passes() {
        throw new \Exception('make sure your logic');
    }
    public function validateDateSpacedFormat($attribute, $value, $parameters) {
        $value = str_replace(' ', '0', $value);
        return parent::validateDateFormat($attribute, $value, $parameters);
    }
}
